export default{
	state:{
		list:[
			{
				id:1,
				checked:false,
				name:"11111111111111111",
				color:"#ffffff",
				imgUrl:"../../static/img/2.webp",
				pprice:"27",
				num:"1",
			},
			{
				id:2,
				checked:false,
				name:"11111111111111111",
				color:"#d3ff43",
				imgUrl:"../../static/img/3.webp",
				pprice:"48",
				num:"2",
			}
		],
		selectedList:[]
	},
	getters:{
		checkedAll(state){
			return state.list.length == state.selectedList.length;
		},
		
		//合计
		totalCount(state){
			let total = {
				pprice:0,
				num:0
			}
			state.list.forEach(v=>{
				// 判断商品是否选中
				if(state.selectedList.indexOf(v.id) > -1){
					total.pprice += v.pprice * v.num
					total.num = state.selectedList.length
				}
			})
			return total
		}
	},
	mutations:{
		// 全选
		checkedAll(state){
			state.selectedList = state.list.map( v=>{
				v.checked = true
				return v.id
			})
		},
		// 全不选
		uncheckedAll(state){
			state.list.forEach(v=>{
				v.checked = false
			})
			state.selectedList = []
		},
		//单选
		selectedItem(state,index){
			
			let id = state.list[index].id
			let i = state.selectedList.indexOf(id)
			// 如果selectedList已存在对应id则进行删除
			if( i > -1){
				state.list[index].checked = false
				return state.selectedList.splice(i,1)
			}
			// 否则添加对应选中的id
			state.list[index].checked =true
			state.selectedList.push(id)
		},
		delGoods(state){
			// 根据返回对应的id 对state中的数据进行删除
			state.list = state.list.filter(v=>{
				// 返回数组中被选中的数据的id
				return state.selectedList.indexOf(v.id) == -1
			})
		}
	},
	actions:{
		// commit--->使用mutions
		checkedAllFn({commit,getters}){
			getters.checkedAll ? commit("uncheckedAll") : commit("checkedAll")
		},
		delGoodsFn({commit}){
			commit('delGoods')
			commit('uncheckedAll')
			uni.showToast({
				title:"删除成功",
				icon:"none"
			})
		}
	}
}